package com.zbkj.dal.common;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author 484
 */
public class BigDecimalSerializer {

    public static final class ConvertToString extends JsonSerializer<BigDecimal> {
        @Override
        public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            if (Objects.isNull(value)) {
                gen.writeNull();
            } else {
                gen.writeString(value.setScale(2, RoundingMode.DOWN).toPlainString());
            }
        }
    }

    public static final class ConvertToInteger extends JsonSerializer<BigDecimal> {
        @Override
        public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            if (Objects.isNull(value)) {
                gen.writeNull();
            } else {
                gen.writeNumber(value.intValue());
            }
        }
    }

}
